package edu.gatech.TNE.wonderLab7000.person;

import java.sql.*;

import edu.gatech.TNE.wonderLab7000.util.WonderLogger;

/**
 * Administrative class for admin methods
 * 
 * @author Team NamingException
 *
 */
public class admin extends person {

	private String accessType, query;
    private Connection conn;
    private ResultSet result;
    private PreparedStatement statement;
    private String[][] allUsers, allCourses;
    private int accessLevel, i, userLength, courseLength;
    public WonderLogger logger = new WonderLogger("mine");
    
	public admin(String userName, int accessLevel) {
		super(userName, accessLevel);
	}
	
	/**
	 * Creates a user and insert them into the database
	 * 
	 * @param username UserID
	 * @param password Password
	 * @param name Name of user
	 * @param access Access level
	 * @return true if user was created, false if not
	 * @throws SQLException
	 */
	public Boolean createUser (String username, String password, String name, int access) 
	throws SQLException {
		
		conn = connect();
		query = "SELECT * FROM USERS WHERE USERID ='" + username + "'";
    	statement = conn.prepareStatement(query);
        try {
			result = statement.executeQuery();
		} catch (SQLException e) {
            conn.close();
            statement.close();
            result.close();
			System.out.println(e.toString());
			logger.error(e.toString());
		} 
		
		if (!result.next()) {
            query = "INSERT INTO USERS (USERID, PASSWORD, ACCESSLEVEL, NAME, ACTIVE) VALUES " +
            		"('" + username + "', '" + password + "', " + access +", '" + name + "', 1)";
        	statement = conn.prepareStatement(query);
            try {
    			result = statement.executeQuery();
    		} catch (SQLException e) {
	            conn.close();
	            statement.close();
	            result.close();
    			System.out.println(e.toString());
    			logger.error(e.toString());
    		} 
        	
            conn.close();
            statement.close();
            result.close();			
			return true;
        }
		else {
            conn.close();
            statement.close();
            result.close();
            return false;
		}
	}
	
	/**
	 * Used for viewing all users
	 * 
	 * @return An array of string arrays of all users
	 * @throws SQLException
	 */
	public String[][] viewUsers () throws SQLException {
		
		conn = connect();
		query = "SELECT * FROM USERS";
    	PreparedStatement statement = conn.prepareStatement(query);
    	allUsers = new String[100][100];
        try {
			result = statement.executeQuery();
		} catch (SQLException e) {
            conn.close();
            statement.close();
            result.close();
			System.out.println(e.toString());
			logger.error(e.toString());
		} 
		
		userLength = 0;
		while (result.next()) {
			userLength++;
		}
		
		allUsers = new String[userLength][3];
		statement = conn.prepareStatement(query);
        try {
			result = statement.executeQuery();
		} catch (SQLException e) {
            conn.close();
            statement.close();
            result.close();
			System.out.println(e.toString());
			logger.error(e.toString());
		} 
		
		i = 0;
		while (result.next()) {
			allUsers[i][0] = result.getString("NAME");
			allUsers[i][1] = result.getString("USERID");
			
			accessLevel = result.getInt("ACCESSLEVEL");
			if (accessLevel == 1) {
				accessType = "Student";
			}
			else if (accessLevel == 2){
				accessType = "Teacher";
			}
			else if (accessLevel == 3){
				accessType = "Administrator";
			}
			
			allUsers[i][2] = accessType;
			i++;
		}
		
        conn.close();
        statement.close();
        result.close();
		return allUsers;
		
	}
	
	/**
	 * Used for viewing all users
	 * 
	 * @return An array of string arrays of all courses
	 * @throws SQLException
	 */
	public String[][] viewCourses () throws SQLException {
	
		conn = connect();
		query = "SELECT * FROM COURSES";
    	statement = conn.prepareStatement(query);
        try {
			result = statement.executeQuery();
		} catch (SQLException e) {
            conn.close();
            statement.close();
            result.close();
			System.out.println(e.toString());
			logger.error(e.toString());
		} 
		
		courseLength = 0;
		while (result.next()) {
			courseLength++;
		}
		
		allCourses = new String[courseLength][4];
		statement = conn.prepareStatement(query);
        try {
			result = statement.executeQuery();
		} catch (SQLException e) {
            conn.close();
            statement.close();
            result.close();
			System.out.println(e.toString());
			logger.error(e.toString());
		} 

		i = 0;
		while (result.next()) {
			allCourses[i][0] = result.getString("COURSENAME");
			allCourses[i][1] = result.getString("TEACHER");
			allCourses[i][2] = result.getString("NUMLABS");
			allCourses[i][3] = result.getString("NUMSTUDENTS");
			i++;
		}
		
        conn.close();
        statement.close();
        result.close();
		return allCourses;
		
	}

	public static void main(String[] args) throws SQLException
	{
		admin admin = new admin("mdye",3);
		String[][] courses = admin.viewCourses();
		System.out.print(courses[1][1]);
	}
	
}